<?php

namespace backend\models\file;

use backend\utils\AppUtil;
use Yii;

/**
 * This is the model class for table "att_extend".
 *
 * @property string $uuid
 * @property string $main_att_id
 * @property string $att_name
 * @property string $att_position
 * @property integer $att_size
 * @property string $att_type
 * @property string $submit_time
 * @property string $file_name
 * @property string $table_field
 * @property integer $status
 */
class AttExtend extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'att_extend';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['uuid', 'main_att_id', 'att_name', 'att_position', 'att_size', 'att_type', 'status', 'table_field'], 'required'],
            [['att_size', 'status'], 'integer'],
            [['submit_time'], 'safe'],
            [['uuid', 'main_att_id'], 'string', 'max' => 36],
            [['att_name', 'file_name', 'table_field'], 'string', 'max' => 200],
            [['att_position'], 'string', 'max' => 500],
            [['att_type'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'uuid' => 'Uuid',
            'main_att_id' => 'Main Att ID',
            'att_name' => '附件名称',
            'att_position' => '附件位置',
            'att_size' => '附件大小',
            'att_type' => '附件类型',
            'submit_time' => '上传时间',
            'file_name' => 'File Name',
            'table_field' => '哪个表的哪个字段',
            'status' => '状态:1 使用;2 注销',
        ];
    }

    /**
     * 附件预览
     */
    public static function fileView($path)
    {
        return Yii::$app->params['fileViewPrefix'] . '/sx' . $path;
    }

    const ONLINE_VIEW_FILE_URL = 'https://doctrans.36ve.com?ssl=1&furl=';

    /**
     * 在线预览文件 url生成
     *
     * @time 2022-11-8
     */
    public static function fileView2($path)
    {
        return self::ONLINE_VIEW_FILE_URL . Yii::$app->request->hostInfo . '/xcgcs' . $path;
    }

    /**
     * @desc
     * @return array|\yii\db\ActiveRecord[]
     * @Date: 2024/11/1
     */
    public static function get_file_by_attachment_id(string $attachment_id): array
    {
        return self::find()->where([
            'main_att_id' => $attachment_id,
            'status' => AppUtil::STATUS_USE
        ])->orderBy(['submit_time' => SORT_DESC])->asArray()->all();
    }
}
